/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package net.jonbuck.tassoo.model.provider;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Formatter;
import java.util.List;

import net.jonbuck.tassoo.model.Task;
import net.jonbuck.tassoo.model.TassooFactory;
import net.jonbuck.tassoo.model.TassooPackage;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemColorProvider;
import org.eclipse.emf.edit.provider.IItemFontProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITableItemColorProvider;
import org.eclipse.emf.edit.provider.ITableItemFontProvider;
import org.eclipse.emf.edit.provider.ITableItemLabelProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * This is the item provider adapter for a {@link net.jonbuck.tassoo.model.Task} object.
 * <!-- begin-user-doc --> <!-- end-user-doc -->
 * @generated
 */
public class TaskItemProvider extends BaseObjectItemProvider implements IEditingDomainItemProvider, IStructuredItemContentProvider,
		ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource, ITableItemLabelProvider, ITableItemColorProvider,
		ITableItemFontProvider, IItemColorProvider, IItemFontProvider {

	private String datePattern = ((SimpleDateFormat) DateFormat.getDateInstance()).toPattern();

	private static final String TASK_FORMAT = "<{0}> - {1}"; //$NON-NLS-1$

	/**
	 * This constructs an instance from a factory and a notifier. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public TaskItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	/**
	 * This adds a property descriptor for the Actual Time feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addActualTimePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_actualTime_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_actualTime_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__ACTUAL_TIME,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Category feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addCategoryPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_category_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_category_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__CATEGORY,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Completed Date feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addCompletedDatePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_completedDate_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_completedDate_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__COMPLETED_DATE,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Container feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addContainerPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_container_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_container_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__CONTAINER,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Due Date feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addDueDatePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_dueDate_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_dueDate_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__DUE_DATE,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Estimated Time feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addEstimatedTimePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_estimatedTime_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_estimatedTime_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__ESTIMATED_TIME,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Position feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addPositionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_position_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_position_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__POSITION,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Priority feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addPriorityPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_priority_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_priority_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__PRIORITY,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Recurrence Indicator feature.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	protected void addRecurrenceIndicatorPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_recurrenceIndicator_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_recurrenceIndicator_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__RECURRENCE_INDICATOR,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Recurrence Settings feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addRecurrenceSettingsPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_recurrenceSettings_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_recurrenceSettings_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__RECURRENCE_SETTINGS,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Reminder Indicator feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addReminderIndicatorPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_reminderIndicator_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_reminderIndicator_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__REMINDER_INDICATOR,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Start Date feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addStartDatePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_startDate_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_startDate_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__START_DATE,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Status feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addStatusPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_status_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_status_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__STATUS,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Task Id feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addTaskIdPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_taskId_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_taskId_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__TASK_ID,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Task Name feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addTaskNamePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_taskName_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_taskName_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__TASK_NAME,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Task Summary feature. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected void addTaskSummaryPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_taskSummary_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_taskSummary_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__TASK_SUMMARY,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Type feature.
	 * <!-- begin-user-doc
	 * --> <!-- end-user-doc -->
	 * @generated
	 */
	protected void addTypePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Task_type_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Task_type_feature", "_UI_Task_type"),
				 TassooPackage.Literals.TASK__TYPE,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}

	/**
	 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s
	 * describing the children that can be created under this object. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

		newChildDescriptors.add
			(createChildParameter
				(TassooPackage.Literals.TASK__ATTACHMENT_CONTAINER,
				 TassooFactory.eINSTANCE.createAttachmentContainer()));

		newChildDescriptors.add
			(createChildParameter
				(TassooPackage.Literals.TASK__COMMENT,
				 TassooFactory.eINSTANCE.createComment()));

		newChildDescriptors.add
			(createChildParameter
				(TassooPackage.Literals.TASK__TASK,
				 TassooFactory.eINSTANCE.createTask()));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EStructuralFeature getChildFeature(Object object, Object child) {
		// Check the type of the specified child object and return the proper feature to use for
		// adding (see {@link AddCommand}) it as a child.

		return super.getChildFeature(object, child);
	}

	/**
	 * This specifies how to implement {@link #getChildren} and is used to
	 * deduce an appropriate feature for an
	 * {@link org.eclipse.emf.edit.command.AddCommand},
	 * {@link org.eclipse.emf.edit.command.RemoveCommand} or
	 * {@link org.eclipse.emf.edit.command.MoveCommand} in
	 * {@link #createCommand}. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			// childrenFeatures.add(TassooPackage.Literals.TASK__ATTACHMENT);
			// childrenFeatures.add(TassooPackage.Literals.TASK__COMMENT);
			childrenFeatures.add(TassooPackage.Literals.TASK__TASK);
		}
		return childrenFeatures;
	}

	/**
	 * @generated NOT
	 */
	public String getColumnText(Object object, int columnIndex) {
		Task task = (Task) object;
		switch (columnIndex) {
		case 0:
			return task.getTaskName();
		case 1:
			return task.getCategory().getValue();
		case 2:
			return task.getPriority().getValue();
		case 3:
			return task.getType().getValue();
		case 4:
			return task.getStatus().getValue();
		case 6:
			return task.getStartDate() != null ? DateFormatUtils.format(task.getStartDate(), datePattern) : StringUtils.EMPTY;
		case 7:
			return task.getDueDate() != null ? DateFormatUtils.format(task.getDueDate(), datePattern) : StringUtils.EMPTY;
		case 8:
			return null;
		case 9:
			return StringUtils.isNotBlank(task.getEstimatedTime()) ? task.getEstimatedTime() : StringUtils.EMPTY;
		case 10:
			return StringUtils.isNotBlank(task.getActualTime()) ? task.getActualTime() : StringUtils.EMPTY;
		}
		return null;
	}

	/**
	 * 
	 */
	public Object getForeground(Object object, int columnIndex) {
		Task task = (Task) object;
		if (task.getStatus() != null && task.getStatus().getAssociatedPercentage() == 100) {
			return SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY);
		}
		return null;
	}

	/**
	 * This returns the property descriptors for the adapted class. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addActualTimePropertyDescriptor(object);
			addCategoryPropertyDescriptor(object);
			addCompletedDatePropertyDescriptor(object);
			addContainerPropertyDescriptor(object);
			addEstimatedTimePropertyDescriptor(object);
			addDueDatePropertyDescriptor(object);
			addPositionPropertyDescriptor(object);
			addPriorityPropertyDescriptor(object);
			addRecurrenceIndicatorPropertyDescriptor(object);
			addRecurrenceSettingsPropertyDescriptor(object);
			addReminderIndicatorPropertyDescriptor(object);
			addStartDatePropertyDescriptor(object);
			addStatusPropertyDescriptor(object);
			addTaskIdPropertyDescriptor(object);
			addTaskNamePropertyDescriptor(object);
			addTaskSummaryPropertyDescriptor(object);
			addTypePropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	/**
	 * This returns the label text for the adapted class. <!-- begin-user-doc
	 * --> <!-- end-user-doc -->
	 * 
	 * @generated_not
	 */
	@Override
	public String getText(Object object) {
		Task task = (Task) object;
		return MessageFormat.format(TASK_FORMAT, new Formatter().format("%05d", task.getId()), task.getTaskName());
	}

	/**
	 * This handles model notifications by calling {@link #updateChildren} to update any cached
	 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
	 * <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * @generated
	 */
	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Task.class)) {
			case TassooPackage.TASK__ACTUAL_TIME:
			case TassooPackage.TASK__CATEGORY:
			case TassooPackage.TASK__COMPLETED_DATE:
			case TassooPackage.TASK__ESTIMATED_TIME:
			case TassooPackage.TASK__DUE_DATE:
			case TassooPackage.TASK__POSITION:
			case TassooPackage.TASK__PRIORITY:
			case TassooPackage.TASK__RECURRENCE_INDICATOR:
			case TassooPackage.TASK__REMINDER_INDICATOR:
			case TassooPackage.TASK__START_DATE:
			case TassooPackage.TASK__STATUS:
			case TassooPackage.TASK__TASK_ID:
			case TassooPackage.TASK__TASK_NAME:
			case TassooPackage.TASK__TASK_SUMMARY:
			case TassooPackage.TASK__TYPE:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case TassooPackage.TASK__ATTACHMENT_CONTAINER:
			case TassooPackage.TASK__COMMENT:
			case TassooPackage.TASK__TASK:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected boolean shouldComposeCreationImage() {
		return true;
	}

}
